package com.luo.backend.controller;

import com.luo.backend.common.ApiResponse;
import com.luo.backend.dto.AdminMerchantDetailResponse;
import com.luo.backend.dto.AdminMerchantListResponse;
import com.luo.backend.dto.UpdateMerchantStatusRequest;
import com.luo.backend.service.AdminMerchantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 管理员-商家管理控制器
 */
@RestController
@RequestMapping("/api/v1/admin")
public class AdminMerchantController {
    
    @Autowired
    private AdminMerchantService adminMerchantService;
    
    /**
     * 获取商家列表
     * GET /api/v1/admin/merchants
     */
    @GetMapping("/merchants")
    public ApiResponse<AdminMerchantListResponse> getMerchantList(
            @RequestParam(required = false) Integer page,
            @RequestParam(required = false) Integer size,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) Integer status,
            @RequestParam(required = false) String sortBy,
            @RequestParam(required = false) String sortOrder) {
        
        AdminMerchantListResponse response = adminMerchantService.getMerchantList(
            page, size, keyword, status, sortBy, sortOrder
        );
        return ApiResponse.success(response);
    }
    
    /**
     * 获取商家详情
     * GET /api/v1/admin/merchants/{id}
     */
    @GetMapping("/merchants/{id}")
    public ApiResponse<AdminMerchantDetailResponse> getMerchantDetail(@PathVariable Long id) {
        AdminMerchantDetailResponse response = adminMerchantService.getMerchantDetail(id);
        return ApiResponse.success(response);
    }
    
    /**
     * 删除商家
     * DELETE /api/v1/admin/merchants/{id}
     */
    @DeleteMapping("/merchants/{id}")
    public ApiResponse<String> deleteMerchant(@PathVariable Long id) {
        adminMerchantService.deleteMerchant(id);
        return ApiResponse.success("删除成功");
    }
    
    /**
     * 更新商家状态（营业中/休息中）
     * PUT /api/v1/admin/merchants/{id}/status
     */
    @PutMapping("/merchants/{id}/status")
    public ApiResponse<String> updateMerchantStatus(
            @PathVariable Long id,
            @Valid @RequestBody UpdateMerchantStatusRequest request) {
        
        adminMerchantService.updateMerchantStatus(id, request.getStatus());
        String message = request.getStatus() == 1 ? "设置为营业中" : "设置为休息中";
        return ApiResponse.success(message);
    }
}

